<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../../css//layui.css"/>
    <style>
        body {
            background-color: #efefef;
        }

        tr, th {
            text-align: center !important;
        }

        table {
            text-align: center;
        }

        .active {
            color: #ff3c3c;
            font-weight: 600;
        }
    </style>
</head>

<body>
<div id="app">
    <div class="layui-panel">
        <div style="padding: 32px;">
            <div class="layui-form">
                <input type="text" name="" placeholder="消费名称" class="layui-input" v-model.trim="name">
                <hr class="ws-space-16">
                <input type="text" name="" placeholder="消费价格" class="layui-input" v-model.number="price">
            </div>
            <button type="button" class="layui-btn layui-btn-normal" @click="add">添加账单</button>

            <table class="layui-table">
                <thead>
                <tr>
                    <th>编号</th>
                    <th>名称</th>
                    <th>消费价格</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(item,index) in list" :key="item.id">
                    <td>{{index + 1}}</td>
                    <td>{{item.name}}</td>
                    <td :class="{active:item.price>=100}">{{item.price.toFixed(2)}}元</td>
                    <td>
                        <button type="button" class="layui-btn layui-btn-danger" @click="deleteGood(item.id)">删除</button>
                    </td>
                </tr>
                </tbody>
            </table>
            <h3>消费总计：{{totalPrice}}</h3>
        </div>
    </div>


</div>
<script src="../../javascript/vue.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.js"></script>
<script>

    const vm = new Vue({
        el: "#app",
        data() {
            return {
                list: [],
                name: "",
                price: "",
            }
        },
        methods: {
            async getGoodsList() {
                const {data: {data: result}} = await axios({
                    url: "https://applet-base-api-t.itheima.net/bill",
                    params: {
                        creator: "陶品奇"
                    }
                })
                this.list = result
            },
            async add() {
                if (this.name && this.price !== "") {
                    const result = await axios({
                        url: "https://applet-base-api-t.itheima.net/bill",
                        method: "POST",
                        data: {
                            creator: "陶品奇",
                            name: this.name,
                            price: this.price
                        }
                    })
                    this.getGoodsList()
                }
            },
            async deleteGood(id) {
                const result = await axios({
                    url: `https://applet-base-api-t.itheima.net/bill/${id}`,
                    method: "DELETE",
                })
                this.getGoodsList()
            }
        },
        created() {
            this.getGoodsList()
        },
        computed: {
            totalPrice() {
                return this.list.reduce((sum, item) => sum += item.price, 0)
            }
        }
    })
</script>
</body>

</html>